package com.ruoyi.business.controller;

import com.ruoyi.business.service.IWsdPaymentRecordsService;
import com.ruoyi.business.service.IWsdRefundTransactionsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.WsdPaymentRecordsDTO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.export.WsdPaymentRecordsExport;
import com.ruoyi.common.core.domain.vo.ChartDataVO;
import com.ruoyi.common.core.domain.vo.WsdPaymentRecordsVO;
import com.ruoyi.common.core.domain.vo.WsdRefundTransactionsVO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * 财务数据显示
 *
 * @author wwq
 * @since 2024-05-20
 */
@RestController
@RequestMapping("wsd_financial")
public class FinancialManagementController extends BaseController {

    @Autowired
    private IWsdPaymentRecordsService wsdPaymentRecordsService;

    @Autowired
    private IWsdRefundTransactionsService wsdRefundTransactionsService;


    /**
     * 查询充值缴费记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WsdPaymentRecordsDTO wsdPaymentRecordsDTO) {
        startPage();
        List<WsdPaymentRecordsVO> list = wsdPaymentRecordsService.selectWsdPaymentRecordsList(wsdPaymentRecordsDTO);
        return getDataTable(list);
    }



    /**
     * 充值统计接口，根据不同类型返回相应的统计数据
     * @param type 统计类型 (1: 年度统计, 0: 月度统计, 2: 时间区间统计)
     * @param startDate 开始日期（可选，仅在 type 为 2 时使用）
     * @param endDate 结束日期（可选，仅在 type 为 2 时使用）
     * @return 统计结果
     */
    @GetMapping("/statistics")
    public AjaxResult getStatistics(@RequestParam Integer type, @RequestParam(required = false) String startDate, @RequestParam(required = false) String endDate) {
        ChartDataVO result;
        switch (type) {
            case 0:
                //月
                result = wsdPaymentRecordsService.getTotalFeeByYear();
                break;
            case 1:
                //年
                result = wsdPaymentRecordsService.getTotalFeeByMonth();
                break;
            case 2:
                //时间区间
                if (startDate == null || endDate == null) {
                    return AjaxResult.error("startDate 和 endDate 为必填项");
                }
                result = wsdPaymentRecordsService.getTotalFeeByDateRange(startDate, endDate);
                break;
            default:
                return AjaxResult.error("无效的类型。允许的值为 月, 年, 时间区间");
        }
        return AjaxResult.success(result);
    }



    /**
     * 查询退款缴费记录列表
     */
    @GetMapping("/listRefund")
    public TableDataInfo listRefund(WsdPaymentRecordsDTO wsdPaymentRecordsDTO) {
        startPage();
        List<WsdRefundTransactionsVO> list = wsdRefundTransactionsService.listRefund(wsdPaymentRecordsDTO);
        return getDataTable(list);
    }


    /**
     * 退款统计接口，根据不同类型返回相应的统计数据
     * @param type 统计类型 (1: 年度统计, 0: 月度统计, 2: 时间区间统计)
     * @param startDate 开始日期（可选，仅在 type 为 2 时使用）
     * @param endDate 结束日期（可选，仅在 type 为 2 时使用）
     * @return 统计结果
     */
    @GetMapping("/getRefundCount")
    public AjaxResult getRefundCount(@RequestParam Integer type, @RequestParam(required = false) String startDate, @RequestParam(required = false) String endDate) {
        ChartDataVO result;
        Long schoolId = null;
        SysUser user = getLoginUser().getUser();
        if (!user.isAdmin()){
            schoolId = user.getSchoolId(); // 获取用户的学校ID
        }
        switch (type) {
            case 0:
                // 按月查询当前年份
                result = wsdRefundTransactionsService.getTotalFeeByYear(schoolId);
                break;
            case 1:
                // 按年查询当前年份
                result = wsdRefundTransactionsService.getTotalFeeByMonth(schoolId);
                break;
            case 2:
                // 自定义时间区间查询
                if (startDate == null || endDate == null) {
                    return AjaxResult.error("startDate 和 endDate 为必填项");
                }
                result = wsdRefundTransactionsService.getTotalFeeByDateRange(startDate, endDate,schoolId);
                break;
            default:
                return AjaxResult.error("无效的类型。允许的值为 月, 年, 时间区间");
        }
        return AjaxResult.success(result);
    }



    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, WsdPaymentRecordsDTO wsdPaymentRecordsDTO)
    {
        List<WsdPaymentRecordsVO> list = wsdPaymentRecordsService.selectWsdPaymentRecordsList(wsdPaymentRecordsDTO);
        ExcelUtil<WsdPaymentRecordsExport> util = new ExcelUtil<WsdPaymentRecordsExport>(WsdPaymentRecordsExport.class);
        Map<Integer, String> feeTypeDescriptions = new HashMap<>();
        feeTypeDescriptions.put(0, "学生餐费用");
        feeTypeDescriptions.put(1, "其他费用");
        Map<Integer, String> rechargeTypeDescriptions = new HashMap<>();
        rechargeTypeDescriptions.put(0, "微信");
        rechargeTypeDescriptions.put(1, "支付宝");
        rechargeTypeDescriptions.put(2, "现金");
        List<WsdPaymentRecordsExport> list2 = list.stream()
                .map(bizUser -> {
                    String feeTypeDescription = feeTypeDescriptions.getOrDefault(bizUser.getFeeType(), "");
                    String rechargeTypeDescription = rechargeTypeDescriptions.getOrDefault(bizUser.getRechargeType(), "");
                    WsdPaymentRecordsExport wsdPaymentRecordsExport = new WsdPaymentRecordsExport();
                    BeanUtils.copyProperties(bizUser, wsdPaymentRecordsExport);
                    wsdPaymentRecordsExport.setFeeType(feeTypeDescription);
                    wsdPaymentRecordsExport.setRechargeType(rechargeTypeDescription);
                    return wsdPaymentRecordsExport;
                }).collect( Collectors.toList());
        util.exportExcel(response, list2, "充值流水数据");
    }
}
